package com.ygxsk.carhome.ui.rice;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.gyf.immersionbar.ImmersionBar;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.response.CustomerTitle;
import com.ygxsk.carhome.ui.ErrorActivity;
import com.ygxsk.carhome.ui.smartgoout.SmartGoRecordHistoryFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;

/*
 * @Author:itchenqi175@163.com
 * @Time:2023-07-26  20:48
 * @Description: 是餐券发放的历史记录 有 全部：未核销：已核销：已过期 等状态的
 *
 * 废弃
 */
public class RiceTicketRecordHistoryActivity extends ErrorActivity {

    @BindView(R.id.tab_layout_smart_go_history)
    TabLayout tabLayoutSmartGoHistory;

    @BindView(R.id.viewpagerinsmartgohistory)
    ViewPager2 viewPagerInSmartGoHistory;


    //装载的fragment
    private ArrayList<Fragment> fragmentLists = new ArrayList<>();
    //装载标题内容
    private ArrayList<CustomerTitle> titleLists = new ArrayList<>();

    //该界面和标题的绑定
    private TabLayoutMediator tabLayoutMediator;

//    private SmartGoRecordHistoryFragment smartGoRecordHistoryFragment;

    //保险客户信息列表
//    private RunManSafetyCustomerInfoFragment runManSafetyCustomerInfoFragment;

    private MaterialDialog materialDialogLoadAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        initloading();

        initViews();

//        gainDatas();

        //顶部的数字是会变化的联动的；
        //接收外界刷新消息
//        if (!EventBus.getDefault().isRegistered(this)) {
//            EventBus.getDefault().register(this);
//        }

    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_smart_go_record_history;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();

        ImmersionBar.with(this)//
                .titleBar(R.id.toolbarygxskout)
                .fitsSystemWindows(true)//解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                //.statusBarColor(R.color.colorPrimary)//
                //.navigationBarColor(R.color.colorPrimary)//
                //.keyboardEnable(true)
                .init();

    }

    @Override
    protected void initToolBar(ToolbarHelper toolbarHelper) {

        //这个可能是动态的，不同身份 是不同标题：服务站经理存款业绩排名
        toolbarHelper.setTitle(getString(R.string.title_smart_go_history));
        toolbarHelper.setVersionName(true, AppUtils.getAppVersionName());
        toolbarHelper.setLayoutClose(false, "", null);
        toolbarHelper.setLayoutLeft(true, R.drawable.backicon, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                methodBack();
            }
        });

        toolbarHelper.setLayoutRight(false, 0, "", null);

//        //判断是运营还是服务站经理 还是二者都是
//        if (mSession.isSiter() && (!mSession.isOperator())) {//仅仅是【服务站经理】
//
////            LogUtils.d(ConstantSign.LOGCAT_NOMAL + "仅仅是【服务站经理】");
//
//            toolbarHelper.setLayoutRight(false, 0, "", null);
//
//        } else if (mSession.isOperator()) {//只要有运营的身份是真，那么就说明【既是运营-又是服务站经理】
//
////            LogUtils.d(ConstantSign.LOGCAT_NOMAL + "肯定是【运营】");
//
//            toolbarHelper.setLayoutRight(true, 1, "添加", new ClickUtils.OnDebouncingClickListener() {
//                @Override
//                public void onDebouncingClick(View v) {
//                    //历史记录的页面布局
//                    Bundle extra_bundle = new Bundle();
//
//                    //携带该条数据的信息 VALUE_JUMP_FORM_CUSTOMER_BANK_ADD
//                    extra_bundle.putInt(ExtraKey.EXTRA_JUMPFROM_WHERE, ExtraValue.VALUE_JUMP_FORM_CUSTOMER_BANK_ADD);
//                    //携带实体过去
//                    //extra_bundle.putParcelable(ExtraKey.EXTRA_WORKBOOKFIRSTNODE, workBookFirstNode);
//                    //这里空携带
//                    ActivityUtils.startActivity(extra_bundle, RunManAddCustomerOpenCardEditActivity.class);
//
//                }
//            });
//        }

    }

    @Override
    protected void onClickNodata(View v) {

        //这里是不写接口的

    }

    @Override
    protected void initViews() {

        //初始化fragments
        if (fragmentLists == null) {
            fragmentLists = new ArrayList<>();
        }

        if (titleLists == null) {
            titleLists = new ArrayList<>();
        }

        CustomerTitle customerTitleAllVerifi = new CustomerTitle();
        customerTitleAllVerifi.setTitleName("全部");

        CustomerTitle customerTitleNotVerifi = new CustomerTitle();
        customerTitleNotVerifi.setTitleName("未核销");

        CustomerTitle customerTitleHasVerifi = new CustomerTitle();
        customerTitleHasVerifi.setTitleName("已核销");

        CustomerTitle customerTitleCanNotVerifi = new CustomerTitle();
        customerTitleCanNotVerifi.setTitleName("已失效");

        titleLists.add(customerTitleAllVerifi);
        titleLists.add(customerTitleNotVerifi);
        titleLists.add(customerTitleHasVerifi);
        titleLists.add(customerTitleCanNotVerifi);

        fragmentLists.add(SmartGoRecordHistoryFragment.newInstance());
        fragmentLists.add(SmartGoRecordHistoryFragment.newInstance());
        fragmentLists.add(SmartGoRecordHistoryFragment.newInstance());
        fragmentLists.add(SmartGoRecordHistoryFragment.newInstance());

        //创建适配器
        MySmartGoHistroyAdapter mySmartGoHistroyAdapter = new MySmartGoHistroyAdapter(this, fragmentLists);
        viewPagerInSmartGoHistory.setAdapter(mySmartGoHistroyAdapter);

        //设置字体颜色，第一个参数代表未点击时的字体颜色，第二个参数代表点击后的颜色
        //tabLayoutInCustomer.setTabTextColors(R.color.black,R.color.teal_700);

        //点击单个tab时背景为透明色
        //tabLayout.setTabRippleColor(ColorStateList.valueOf(Color.TRANSPARENT));

        //这种是不是将来还需要接口回来，重新绑定
        //让viewpager2与tablayout关联，实现滑动效果（滑动fragment底部tab变化，tab滑动fragment跟着滑动）
        //默认都是0
//        method_tabText();


        //这种是不是将来还需要接口回来，重新绑定
        //让viewpager2与tablayout关联，实现滑动效果（滑动fragment底部tab变化，tab滑动fragment跟着滑动）
        tabLayoutMediator = new TabLayoutMediator(tabLayoutSmartGoHistory, viewPagerInSmartGoHistory, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                //标题和数量的合并
                tab.setText(titleLists.get(position).getTitleName());
            }
        });

        tabLayoutMediator.attach();//一定要attach（）

    }

    @Override
    protected void gainDatas() {

        //

    }

    @Override
    protected void methodBack() {
        if (ActivityUtils.isActivityAlive(this)) {
            ActivityUtils.finishActivity(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private class MySmartGoHistroyAdapter extends FragmentStateAdapter {


        private ArrayList<Fragment> fragmentList;

        public MySmartGoHistroyAdapter(@NonNull FragmentActivity fragmentActivity, ArrayList<Fragment> fragments) {
            super(fragmentActivity);
            this.fragmentList = fragments;
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getItemCount() {
            return fragmentList.size();
        }


    }
}